
package com.yimixian.app.qqwallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mobileqq.openpay.api.IOpenApi;
import com.tencent.mobileqq.openpay.api.IOpenApiListener;
import com.tencent.mobileqq.openpay.api.OpenApiFactory;
import com.tencent.mobileqq.openpay.data.base.BaseResponse;
import com.tencent.mobileqq.openpay.data.pay.PayResponse;
import com.yimixian.app.R;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.QQWalletRespsonse;
import com.yimixian.app.order.AbstractOrderActivity;

import de.greenrobot.event.EventBus;

/**
 * Created by songzy on 2015/10/30.
 */
public class CallbackActivity extends Activity implements IOpenApiListener {

    String appId = Constants.QQ_WALLET_APP_ID;
    IOpenApi openApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qqwallet_callback);

        openApi = OpenApiFactory.getInstance(this, appId);
        openApi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        openApi.handleIntent(intent, this);
    }

    @Override
    public void onOpenResponse(BaseResponse response) {
        String message = "";
        if (response == null) {
            DataManager.getInstance().put(Constants.QQ_WALLET_RESPONSE_CODE,
                    false);
            return;
        } else {
            if (response instanceof PayResponse) {
                PayResponse payResponse = (PayResponse) response;
                QQWalletRespsonse qqResponse = new QQWalletRespsonse();
                qqResponse.response = payResponse.isSuccess();
                EventBus.getDefault().post(qqResponse);
            } else {
                DataManager.getInstance().put(Constants.QQ_WALLET_RESPONSE_CODE,
                        false);
            }
        }
        finish();
    }
}
